<template>
  <div class="auto">
    <validator name='v'>
      <form novalidate class="form-horizontal">
        <div  style="margin-top:10px" :class="[$v.f_tablebase.dctest ? 'has-error' : 'has-success']">
          <label for="f_tablebase"  class=" col-sm-2 control-label">表底数:</label>
          <div class="col-sm-3">
            <input class="form-control" type="number" v-model="model.f_tablebase"
             v-validate:f_tablebase='{dctest: [row.f_meter_base, ">=" ] }'  placeholder="表底数" :disabled="disabled">
             <span v-if="$v.f_tablebase.dctest" style="color:red ">本次表底数不能小于上次表底数</span>
          </div>
        </div>
      </form>
      <div class="col-sm-4" style="margin-left:8%" v-if="!message">
        <button class="btn btn-success" @click="confirm()"  :disabled='!$v.valid' id='comprehen-comOpe-chaMet-macMet-确认'>确&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;认</button>
        <button class="btn btn-default" @click="clean()" style="width:80px;margin-left:20px;" id='comprehen-comOpe-chaMet-macMet-取消'>取&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;消</button>
      </div>
    </validator>
  </div>
</template>
<script>
/**
*换表管理
*处理表类型为机表和物联网表的旧表信息
*/
import AppData from '../../../../../stores/AppData'
import co from 'co'
import Vue from 'vue'

let validatorGen = function * (self) {
  let hasHand = yield Vue.resetpost('rs/sql/singleTable', {data: {tablename: 't_handplan', condition: `f_meter_state = '未抄表' and f_user_id = '${self.model.f_user_id}'`}}, {resolveMsg: null, rejectMsg: null})
  // 如果是总表判断分表是否已抄
  console.log('换标，看有没有总分表', self.row.f_totalsplit_type)
  let getSplitHand = false
  if (self.row.f_totalsplit_type === '总表') {
    getSplitHand = yield self.$HandService.getSplitInfo(self.row.f_user_id)
  }
  if (hasHand.data.length > 0) {
    console.log('有未抄表信息')
    self.row.errorType = 'hand'
    self.$dispatch('firstsuccess', 'hand', self.row)
  } else if (hasHand.data.length === 0 && getSplitHand) {
    console.log('有分表没有抄表')
    self.row.errorType = 'split'
    self.$dispatch('firstsuccess', 'hand', self.row)
  } else if (hasHand.data.length === 0 && !getSplitHand){
    console.log('杀杀杀杀杀杀杀杀杀')
    let machineOwe = yield self.$ChangeMeterService.changeMeter(self.model, self.row)

    if (machineOwe.data.isAutoAcc === '是') {
      self.row.isHasOwe = false
      console.log('自动下账了，，，', machineOwe.data.row)
      self.$dispatch('firstsuccess', 'nohand', machineOwe.data.row)
    } else {
      machineOwe.data.row.errorType = 'hasowe'
      self.row.isHasOwe = true
      self.$dispatch('firstsuccess', 'hand', machineOwe.data.row)
    }
  }
}

export default {
  data () {
    return {
      model: {},
      disabled: false
    }
  },
  props: ['row', 'message'],
  watch: {
    'row' (val) {
      this.model.f_user_id = val.f_user_id
      this.model.f_userinfo_id = val.f_userinfo_id
    }
  },
  ready () {
    this.model.f_user_id = this.row.f_user_id
    this.model.f_userinfo_id = this.row.f_userinfo_id
  },
  methods: {
    confirm () {
      this.model.lazy = AppData.getSingleValue('滞纳期限')
      if (this.model.f_tablebase > this.row.f_meter_base) {
        let validator = validatorGen(this)
        co(validator)
        // this.$ChangeMeterService.changeMeter(this.model, this.row).then((res) => {
        //   if (res.data.isAutoAcc === '是') {
        //     this.$dispatch('firstsuccess', 'nohand', null)
        //   } else {
        //     this.$dispatch('firstsuccess', 'hand', res.data.row)
        //     this.disabled = true
        //   }
        // }).catch((error) => {
        //   this.$dispatch('error', '换表管理', this.data, error)
        // })
      } else if ((this.model.f_tablebase - 0) - this.row.f_meter_base === 0) {
        this.$ChangeMeterService.isHasOwe(this.row).then((res) => {
          console.log('换表底数相同。。', res)
          if (res.owe.length > 0) {
            this.row.errorType = 'owe' // 事件通知这是欠费的
            this.row.isHasOwe = true
            this.$dispatch('firstsuccess', 'nohand', this.row)
            this.disabled = true
          } else if (res.hasHandplan.length > 0) {
            this.row.errorType = 'hand' // 事件通知这是有未抄表的
            this.$dispatch('firstsuccess', 'hand', this.row)
            this.disabled = true
          } else {
            this.row.isHasOwe = false
            this.$dispatch('firstsuccess', 'nohand', null)
          }
        }).catch((error) => {
          this.$dispatch('error', '换表管理', this.data, error)
        })
      } else {
        this.row.isHasOwe = false
        this.$dispatch('firstsuccess', 'nohand', null)
      }
    },
    clean () {

      this.$dispatch('clean', this.row)
    }
  }
}
</script>
